package Thread_;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 *工厂模式
 */
    //工厂类：
class makePoint{
    public static Point makePointByXY(int x, int y){
        Point p=new Point();
        p.setX(x);
        p.setY(y);
        return p;
    }
    public static Point makePontByRA(int a,int r){
        Point p=new Point();
        p.setR(r);
        p.setA(a);
        return p;
    }
}

/**
 * 通过不同的方法 创建一个点 通过工厂模式来完成
 */
class Point{
    private int x;
    private int y;
    private int r;
    private int a;

    //这样无法实现重载
//    public Point(int x,int y){}
//    public Point(int a,int r){}

    public Point() {
    }
    //工厂方法：
    public static Point makePointByXY(int x, int y){
        Point p=new Point();
        p.setX(x);
        p.setY(y);
        return p;
    }
    public static Point makePontByRA(int a,int r){
        Point p=new Point();
        p.setR(r);
        p.setA(a);
        return p;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }
}
/**
 * 线程池
 * 模拟实现
 *
 */
class MyThreadPoolExecutors{
    private List<Runnable> list=new ArrayList<>();

    private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<Runnable>(1000);

    public MyThreadPoolExecutors(int n) throws InterruptedException {//n:创建线程的个数
        for(int i=0;i<n;i++){
            new Thread(()->{
                //该线程 频繁扫描阻塞队列queue,
                while(true){
                    try {
                        //扫描到一个,就执行一个
                        Runnable runnable= null;
                         runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);//将任务放到阻塞队列中
    }

}

class MyThreadPoolExecutor{
                                                //指定队列容量为1000
    BlockingQueue<Runnable> queue=new ArrayBlockingQueue<>(1000);
    public MyThreadPoolExecutor(int n){//n:指定线程池中的线程数量
        for(int i=0;i<n;i++){
            new Thread(()->{
                try {
                    //从队列中获取线程，并执行
                    //该队列是带有阻塞功能的，若队列为空，就会进入阻塞等待
                    Runnable runnable = queue.take();
                    runnable.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }
    }
    public void submit(Runnable runnable){
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
public class Thread22 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        for(int i=0;i<1000;i++){
            int n=i;
            Thread t=new Thread(()->{
                System.out.println("当前任务为： "+n+" ,执行的线程名为： "+Thread.currentThread().getName());
            });
            t.start();//注意创建后，要启动线程
        }
    }
    public static void main2(String[] args) throws InterruptedException {
        MyThreadPoolExecutors poolExecutors = new MyThreadPoolExecutors(4);
        for(int i=0;i<1000;i++){
            int n=i;//防止在Runnable接口的run方法中,触发变量捕获,
                    //这里将i赋值给n;每次循环n都是新的值,就能避免变量捕获
            poolExecutors.submit(()->{
                System.out.println("执行线程"+ n+" ,线程名为: "+Thread.currentThread().getName());
            });

        }
    }
    public static void main1(String[] args) {
        ExecutorService threadPool= Executors.newFixedThreadPool(4);
        threadPool.submit(new Runnable(){
            @Override
            public void run() {
                System.out.println("线程池 ");
            }
        });
    }
}
